package com.weigang.redis.controller;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/test")
public class TestController {

    /* redis常量key */
    public static final String REDIS_KEY = "test:_w";


    @Resource
    private RedisTemplate redisTemplate;



    //请求地址：localhost:8080/test/add
    @GetMapping("/add")
    public void add(){
        //当前时间时间戳
        String nowStr = String.valueOf(System.currentTimeMillis());
        //设置redis中的key值
        String redisKey = REDIS_KEY.concat(nowStr);
        //向对应的key值添加数据
        redisTemplate.opsForValue().set(REDIS_KEY,"测试向redis中添加数据");
        //设置过期时间
        redisTemplate.expire(REDIS_KEY,20, TimeUnit.SECONDS);
    }

    @GetMapping("/get")
    public void get(){
        //获取redis中的数据
        String str = (String) redisTemplate.opsForValue().get(REDIS_KEY);
        System.out.println("获取redis中的数据-->" + str);
    }

}
